home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-16 | 16.2 KB | 487 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Dialog.cpp
- // Release Version: $ ODF 1 $
- //
- // Author: Laurent Delamare
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "Form.hpp"
-
- #ifndef DIALOG_H
- #include "Dialog.h"
- #endif
-
- #ifndef PART_H
- #include "Part.h"
- #endif
-
- #ifndef FRAME_H
- #include "Frame.h"
- #endif
-
- #ifndef FORMVIEW_H
- #include "FormView.h"
- #endif
-
- #ifndef DEFINES_K
- #include "Defines.k"
- #endif
-
- // ----- Framework Layer -----
-
- #ifndef FWEDVIEW_H
- #include "FWEdView.h"
- #endif
-
- #ifndef FWSTATIC_H
- #include "FWStatic.h"
- #endif
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- #ifndef FWCONTXT_H
- #include "FWContxt.h"
- #endif
-
- #ifndef FWBUTTON_H
- #include "FWButton.h"
- #endif
-
- #ifndef FWKEYF_H
- #include "FWKeyF.h"
- #endif
-
- #ifndef FWNOTDEF_H
- #include "FWNotDef.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWMENU_H
- #include "FWMenu.h"
- #endif
-
- #ifndef FWEVENT_H
- #include "FWEvent.h"
- #endif
-
- #ifndef FWALERT_H
- #include "FWAlert.h"
- #endif
-
- #ifndef SLMixOS_H
- #include "SLMixOS.h"
- #endif
-
- #ifndef FWCFMRES_H
- #include "FWCFMRes.h"
- #endif
-
- // ----- Graphic Includes -----
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- #ifndef FWTXTBOX_H
- #include "FWTxtBox.h"
- #endif
-
- #ifndef FWRECSHP_H
- #include "FWRecShp.h"
- #endif
-
- // ----- Foundation Layer -----
-
- #ifndef FWEXCDEF_H
- #include <FWExcDef.h>
- #endif
-
- #ifndef FWNOTIFN_H
- #include "FWNotifn.h"
- #endif
-
- //========================================================================================
- // Runtime Informations
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfform
- #endif
-
- FW_DEFINE_AUTO(CPwdDialogFrame)
- FW_DEFINE_AUTO(CPwdEditView)
-
- //========================================================================================
- // class CPwdDialogFrame
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::CPwdDialogFrame
- //----------------------------------------------------------------------------------------
- CPwdDialogFrame::CPwdDialogFrame(Environment* ev, ODFrame* odFrame,
- FW_CPresentation* presentation, CFormPart* part, FW_ResourceId id)
- : FW_CDialogFrame(ev, odFrame, presentation, part, id)
- {
-
- // If your modal dialog contains edit views which support Undo/Redo like CScrollEditView
- // you should uncomment the following line to mark the Undo & Redo stacks
- // This way you can clear the action history when the dialog is deleted (see below).
- // part->GetSession(ev)->GetUndo(ev)->MarkActionHistory(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::~CPwdDialogFrame
- //----------------------------------------------------------------------------------------
-
- CPwdDialogFrame::~CPwdDialogFrame()
- {
- // If your modal dialog contains edit views which support Undo/Redo like CScrollEditView
- // you should uncomment the following line to clear the Undo & Redo stacks
- // GetPart(ev)->GetSession(ev)->GetUndo(ev)->ClearActionHistory(ev, kODRespectMarks);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::FacetAdded
- //----------------------------------------------------------------------------------------
- // On the Mac we need to add a color table to the dialog window in order to draw
- // a uniform gray background (otherwise the dialog frame keeps a white border)
-
- #ifdef FW_BUILD_MAC
-
- void CPwdDialogFrame::FacetAdded(Environment* ev, ODFacet* facet, unsigned short facetCount)
- {
- FW_CDialogFrame::FacetAdded(ev, facet, facetCount);
-
- if (facetCount == 1)
- {
- ODPlatformWindow platformWindow = facet->GetWindow(ev)->GetPlatformWindow(ev);
-
- WCTabHandle newColorTable = (WCTabHandle)FW_CMemoryManager::AllocateSystemHandle
- (8+sizeof(ColorSpec));
- (*newColorTable)->ctSize = 0;
- (*newColorTable)->ctTable[0].value = wContentColor;
- (*newColorTable)->ctTable[0].rgb = FW_CColor(FW_kRGBLightGray);
- ::SetWinColor(platformWindow, newColorTable);
- }
- }
- #else
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::Draw
- //----------------------------------------------------------------------------------------
-
- void CPwdDialogFrame::Draw(Environment *ev, ODFacet* odFacet, ODShape* invalidShape)
- {
- // Set drawing contect for the CFrame
- FW_CViewContext vc(ev, this, odFacet, invalidShape);
-
- // Draw gray background
- FW_CRectShape::RenderRect(vc, GetBounds(ev), FW_kFill, FW_kRGBLightGray);
-
- }
- #endif // FW_BUILD_MAC
-
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::PostCreateViewFromStream
- //----------------------------------------------------------------------------------------
- // PostCreateViewFromStream is called after subviews are created from resources.
- // Implement initializations for this view that can't be done from a stream.
-
- void CPwdDialogFrame::PostCreateViewFromStream(Environment* ev)
- {
- // ----- Call inherited to propagate to all subviews if necessary
- FW_CSuperView::PostCreateViewFromStream(ev);
-
- // ----- Add alpha-numeric keyfilters to edit views
- // (will be deleted along with the edit views, they are adopted event handlers)
- FW_CView* editView1 = FindViewById(ev, kPasswordEditID);
- FW_CView* editView2 = FindViewById(ev, kConfirmEditID);
-
- FW_CAlphaNumFilter* keyF1 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView1);
- FW_CAlphaNumFilter* keyF2 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView2);
-
- // Note: OK and Cancel buttons are linked automatically to the dialogFrame by
- // defining FW_kDefaultButtonMsg and FW_kCancelButtonMsg in "Views.fr"
-
- // WARNING: Make sure that classes created from resources won't be dead-stripped
- // (here we don't need to worry about our custom class CPwdEditView
- // because all its implementation is compiled in this file)
- FW_DO_NOT_DEAD_STRIP(FW_CButton);
- FW_DO_NOT_DEAD_STRIP(FW_CStaticText);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::CreateSubViews
- //----------------------------------------------------------------------------------------
-
- // NOTE: CreateSubViews is not needed when the frame's ctor is passed a resource id.
- // This section was left in the sample to show how to create views by program
- // instead of using resources.
- #if 0
- void CPwdDialogFrame::CreateSubViews(Environment* ev)
- {
- FW_CRect viewRect; // working rectangle for creating the views
-
- // ---- Create the static texts
- viewRect.Set(FW_IntToFixed(20), FW_IntToFixed(40), FW_IntToFixed(140), FW_IntToFixed(70));
- FW_CStaticText* staticText1 = FW_NEW(FW_CStaticText, (ev, this, viewRect, FW_CString("Pick a \rpassword:")));
-
- viewRect.Set(FW_IntToFixed(20), FW_IntToFixed(70), FW_IntToFixed(140), FW_IntToFixed(100));
- FW_CStaticText* staticText2 = FW_NEW(FW_CStaticText, (ev, this, viewRect, FW_CString("Retype to \nverify:")));
-
- // ---- Create the text edit controls using our custom Edit Views
- viewRect.Set(FW_IntToFixed(150), FW_IntToFixed(40), FW_IntToFixed(280), FW_IntToFixed(60));
- CPwdEditView* editView1 = FW_NEW(CPwdEditView, (ev, this, kPasswordEditID, viewRect));
-
- viewRect.Set(FW_IntToFixed(150), FW_IntToFixed(70), FW_IntToFixed(280), FW_IntToFixed(90));
- CPwdEditView* editView2 = FW_NEW(CPwdEditView, (ev, this, kConfirmEditID, viewRect));
-
- // ----- Add alpha-numeric keyfilters to edit views
- FW_CAlphaNumFilter* keyF1 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView1);
- FW_CAlphaNumFilter* keyF2 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView2);
-
- // ----- Create the OK/Cancel buttons:
- // - Use normal font instead of default system font
- // - Set the control message so that they are initialized as "default" and "cancel"
- // dialog buttons (no need to call SetDefaultButton() and SetCancelButton() later)
- viewRect.Set(FW_IntToFixed(180), FW_IntToFixed(117), FW_IntToFixed(260), FW_IntToFixed(148));
- FW_CButton* okButton = FW_NEW(FW_CButton, (ev, this, kOKButtonID, viewRect, FW_kPushButton,
- FW_CString("OK"), FW_kNormalFont, FW_kDefaultButtonMsg));
-
- viewRect.Set(FW_IntToFixed(60), FW_IntToFixed(123), FW_IntToFixed(140), FW_IntToFixed(145));
- FW_CButton* cancelButton = FW_NEW(FW_CButton, (ev, this, kCancelButtonID, viewRect, FW_kPushButton,
- FW_CString("Cancel"), FW_kNormalFont, FW_kCancelButtonMsg));
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::Initialize
- //----------------------------------------------------------------------------------------
- // You can group all additional dialog initialization in a local method like this one
- // or simply put the code after your call to FW_CDialogFrame::NewModalDialog
-
- void CPwdDialogFrame::Initialize(Environment* ev, CFormFrame* frame)
- {
- fFormFrame = frame;
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdDialogFrame::HandleNotification
- //----------------------------------------------------------------------------------------
-
- void CPwdDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
- {
- // ---- Verify edit fields when clicking on OK button ----
- if (notification.GetMessage() == FW_kDefaultButtonMsg)
- {
- // Get the 2 edit views
- FW_CView* view = FindViewById(ev, kPasswordEditID);
- CPwdEditView* pwdTEd = FW_DYNAMIC_CAST(CPwdEditView, view);
- FW_ASSERT(pwdTEd);
- view = FindViewById(ev, kConfirmEditID);
- CPwdEditView* confirmTEd = FW_DYNAMIC_CAST(CPwdEditView, view);
- FW_ASSERT(confirmTEd);
-
- // Check that the password has at least 3 characters
- FW_CString text = pwdTEd->GetText(ev);
-
- if (text.GetCharacterLength() < 3) {
- // Open Alert and return to avoid closing the dialog
- FW_ErrorAlert(FW_CString(), FW_CString("Password must contain at least 3 characters"));
- return;
- }
-
- // Check that both strings are equal
- FW_CString text2 = confirmTEd->GetText(ev);
-
- if (text != text2) {
- // Open Alert and return to avoid closing the dialog
- FW_ErrorAlert(FW_CString(), FW_CString("Please re-type, strings don't match"));
- return;
- }
-
- // Password is OK, we can switch the form.
- CFormView* formView = (CFormView*) fFormFrame->GetContentView(ev);
- formView->SwitchForm(ev);
- }
-
- // Must call inherited to close the dialog on OK & Cancel
- // WARNING: do not do anything else after this call... the dialog is gone!
- FW_CDialogFrame::HandleNotification(ev, notification);
- }
-
- //========================================================================================
- // class CPwdEditView
- //========================================================================================
-
- FW_DEFINE_CLASS_M1(CPwdEditView, FW_CEditView);
-
- const FW_ClassTypeConstant LPwdEditView = FW_TYPE_CONSTANT('P','w','d','v');
- FW_REGISTER_ARCHIVABLE_CLASS(LPwdEditView, CPwdEditView, CPwdEditView::Create, FW_CView::Read, CPwdEditView::Destroy, FW_CView::Write)
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::CPwdEditView
- //----------------------------------------------------------------------------------------
- CPwdEditView::CPwdEditView(Environment* ev, FW_CSuperView* container, ODID viewId,
- const FW_CRect& bounds)
- : FW_CEditView(ev, container, viewId, bounds)
- {
- Initialize(ev, container, bounds);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::CPwdEditView
- //----------------------------------------------------------------------------------------
- CPwdEditView::CPwdEditView(Environment* ev)
- : FW_CEditView(ev)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::~CPwdEditView
- //----------------------------------------------------------------------------------------
-
- CPwdEditView::~CPwdEditView()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::Initialize
- //----------------------------------------------------------------------------------------
-
- void CPwdEditView::Initialize(Environment* ev, FW_CSuperView* container, const FW_CRect& bounds)
- {
- FW_CRect farBounds(bounds);
- farBounds.Place(FW_IntToFixed(30000), FW_IntToFixed(30000));
-
- // Create a shadow edit view to hold the real text
- // (Note: Making the view invisible is not enough to hide it because the Mac toolbox
- // will still draw the text being inserted with TEKey)
- fShadowTEd = FW_NEW(FW_CEditView, (ev, container, kNoIdentifier, farBounds));
- fShadowTEd->Disable(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::Create
- //----------------------------------------------------------------------------------------
-
- void* CPwdEditView::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
- {
- FW_UNUSED(stream);
- FW_UNUSED(type);
- FW_SOMEnvironment ev;
- return FW_NEW(CPwdEditView, (ev));
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::Destroy
- //----------------------------------------------------------------------------------------
-
- void CPwdEditView::Destroy(void* object, FW_ClassTypeConstant type)
- {
- FW_UNUSED(type);
- CPwdEditView* self = (CPwdEditView*) object;
- delete self;
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::Flatten
- //----------------------------------------------------------------------------------------
-
- void CPwdEditView::Flatten(Environment* ev, FW_CWritableStream& archive) const
- {
- FW_CEditView::Flatten(ev, archive);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::InitializeFromStream
- //----------------------------------------------------------------------------------------
-
- void CPwdEditView::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
- {
- FW_CEditView::InitializeFromStream(ev, stream);
-
- Initialize(ev, GetSuperView(ev), GetBounds(ev));
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::ActivateTarget
- //----------------------------------------------------------------------------------------
-
- void CPwdEditView::ActivateTarget (Environment * ev, FW_Boolean tabSelection)
- {
- // must select also the shadow TEd after a Tab
- if (tabSelection)
- fShadowTEd->SelectAll(ev);
-
- FW_CEditView::ActivateTarget(ev, tabSelection);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::DoMenu
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CPwdEditView::DoMenu (Environment* ev, const FW_CMenuEvent& event)
- {
- switch (event.GetCommandID(ev)) {
- case kODCommandCut:
- case kODCommandCopy:
- case kODCommandPaste:
- case kODCommandClear:
- return TRUE;
- case kODCommandSelectAll:
- SelectAll(ev);
- break;
- default:
- return FW_CView::DoMenu (ev, event);
- }
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::DoCharKey
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CPwdEditView::DoCharKey (Environment* ev, const FW_CCharKeyEvent & event)
- {
- #ifdef FW_BUILD_MAC
- char bullet = 0xA5;
-
- if (event.GetModifiers() & cmdKey)
- return false;
- #endif
- #ifdef FW_BUILD_WIN
- char bullet = '*';
- #endif
-
- // For printable chars use bullet in visible TEd instead of real char
- char realChar = event.GetChar(ev);
- char pwdChar = (realChar >= 0x20) ? bullet : realChar;
-
- // Make Backspace erase the whole text
- if (realChar == 0x08) {
- SelectAll(ev);
- fShadowTEd->SelectAll(ev);
- }
-
- InputCharKey(ev, pwdChar);
- return fShadowTEd->InputCharKey(ev, realChar);
- }
-
- //----------------------------------------------------------------------------------------
- // CPwdEditView::GetText
- //----------------------------------------------------------------------------------------
-
- FW_CString CPwdEditView::GetText (Environment * ev)
- {
- return fShadowTEd->GetText(ev);
- }
-
-
-